from google.appengine.ext import db
from puremvc.patterns.proxy import Proxy
import vo


class MpProxy(Proxy):
    NAME = "MpProxy"

    STATES_LONG = [
        'Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado',
        'Connecticut', 'Delaware', 'District Of Columbia', 'Florida',
        'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas',
        'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachusetts',
        'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana',
        'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico',
        'New York', 'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma',
        'Oregon', 'Pennsylvania', 'Rhode Island', 'South Carolina',
        'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia',
        'Washington', 'West Virginia', 'Wisconsin', 'Wyoming', 'Alberta',
        'British Columbia', 'Manitoba', 'New Brunswick',
        'Newfoundland and Labrador', 'Northwest Territories', 'Nova Scotia',
        'Nunavut', 'Ontario', 'Prince Edward Island', 'Quebec', 'Saskatchewan',
        'Yukon']

    STATES_SHORT = [
        'AL', 'AK', 'AZ', 'AR', 'CA', 'CO', 'CT', 'DE', 'DC', 'FL', 'GA', 'HI',
        'ID', 'IL', 'IN', 'IA', 'KS', 'KY', 'LA', 'ME', 'MD', 'MA', 'MI', 'MN',
        'MS', 'MO', 'MT', 'NE', 'NV', 'NH', 'NJ', 'NM', 'NY', 'NC', 'ND', 'OH',
        'OK', 'OR', 'PA', 'RI', 'SC', 'SD', 'TN', 'TX', 'UT', 'VT', 'VA', 'WA',
        'WV', 'WI', 'WY', 'AB', 'BC', 'MB', 'NB', 'NL', 'NT', 'NS', 'NU', 'ON',
        'PE', 'QC', 'SK', 'YT']

    COUNTRIES_LONG = [
        'USA', 'Canada']

    COUNTRIES_SHORT = [
        'US', 'CA']

    def __init__(self):
        super(MpProxy, self).__init__(MpProxy.NAME, [])
        self.data = []

    def get_all_mps(self):
        return self.data

    def get_mps_array(self):
        for mp in self.data:
            one_mp = []
            one_mp.append(mp.key)
            one_mp.append(mp.title)
            one_mp.append(mp.street)
            one_mp.append(mp.suite)
            one_mp.append(mp.city)
            one_mp.append(mp.state)
            one_mp.append(mp.zip)
            one_mp.append(mp.country)
            one_mp.append(mp.lat)
            one_mp.append(mp.lng)
            one_mp.append(mp.phone)

    def retrieve_all_mps(self):
        # Get all MP's from datastore.
        query = vo.MpVo.all()
        query.order("-mtime")

        # Populate them into self contained data array.
        self.data = []
        for mp in query:
            self.data.append(mp)

    def add_mp(self, fdict):
        pdict = self._build_mp_dict(fdict)
        mp = vo.MpVo(**pdict)
        mp.put()

    def _build_mp_dict(self, fdict):
        pdict = {}

        if fdict.has_key('cname'):
            if fdict.has_key('ename'):
                pdict['title'] = ''.join((fdict['cname'], ' (', fdict['ename'], ')'))
            else:
                pdict['title'] = fdict['cname']
        else:
            pdict['title'] = fdict['ename']
